package com.cqust.controller;

import com.cqust.pojo.Admin;
import com.cqust.service.AdminService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;

@Controller
public class AdminController {

    @Autowired
    private AdminService adminService;

    @RequestMapping("/toLogin")
    public String toLogin(){
        return "login";
    }

    @RequestMapping("/toRegister")
    public String toRegister(){
        return "register";
    }

    @ResponseBody
    @RequestMapping("/login")
    public String login(String eid, String password, HttpSession session){
        boolean IsMatch= adminService.login(eid, password);
        if(IsMatch){
            Admin admin = adminService.getAdminByEid(eid);
            session.setAttribute("Name",admin.getName());
            session.setAttribute("Role",admin.getRole());
            session.setAttribute("Eid",admin.getEid());
            return "true";
        };
        return "false";
    }
    @ResponseBody
    @RequestMapping("/register")
    public String register(Admin admin,@RequestParam("passwordTwo") String passwordTwo){
        boolean register = adminService.register(admin, passwordTwo);
        if(register) return "true";
        return "false";
    }

    @RequestMapping("/logout")
    public String logout(HttpSession session){
        session.removeAttribute("Admin");
        return "login";
    }
}
